package com.zj.dao;

import com.zj.Domain.User;
import org.apache.commons.codec.cli.Digest;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

public class UserDao {
    private static List<User> users = new ArrayList<>();

    static {
        users.add(new User("admin", DigestUtils.md5Hex("123456")));
    }

    public int addUser(User user) {
        boolean add = users.add(user);
        return users.size();

    }

    public User find(String username) {
        Optional<User> any = users.stream().filter(u ->
                u.getUsername().equals(username)).findAny();
        User ret = any.isPresent() ? any.get() : null;
        return ret;
    }

    public User find(String username, String password) {
        User auser = find(username);
        if (auser != null && auser.getPassword().equals(DigestUtils.md5Hex(password))) {
            return auser;
        }
        return null;
    }
}
